﻿using System;
using System.Text;
using System.Windows.Forms;
using BigTestV3.xmlHelp;

namespace BigTestV3
{
    public class HandlerAnswerFactory
    {
        /// <summary>
        /// 根据Kinds来解析对应的题型
        /// </summary>
        /// <param name="id">paperDetailId</param>
        /// <param name="kind">kind</param>
        /// <param name="element">节点元素</param>
        /// <returns>每题的答案</returns>
        public  static TestDetail SetAnswerByKinds(string tempId,string kind,string testlibraryId,HtmlElement element)
        {
            int index = testlibraryId.IndexOf('_');
            string id = testlibraryId.Substring(index+1, testlibraryId.Length - index-1);
            testlibraryId = testlibraryId.Replace("_"+id, "");
            if (string.Equals(kind, "K206"))
            {                                                                               
                return SetAnswerByK206(id,testlibraryId, element);
            }
            else if (string.Equals(kind, "K401") || string.Equals(kind, "K103") || string.Equals(kind, "K201"))
            {
                return SetAnswerK401(id, testlibraryId, element);
            }
            else if (string.Equals(kind, "K503") || string.Equals(kind, "K701") || string.Equals(kind, "K702") || string.Equals(kind, "K408"))
            {
                return SetAnswerK503(id, testlibraryId, element);
            }
            else if (string.Equals(kind, "K403")|| string .Equals(kind,"204")||string .Equals(kind,"K409"))
            {
                return SetAnswerK403(id, testlibraryId, element);
            }
            else if (string.Equals(kind, "K802")||string .Equals(kind,"K801"))
            {
                return SetAnswerK802(id, testlibraryId, element);
            }
            else if (string.Equals(kind, "K102"))
            {
                return SetAnswerK102(id, testlibraryId, element);
            }
            else if (string.Equals(kind, "K202"))
            {
                return SetAnswerK202(id, testlibraryId, element);
            }
            return null;
        }

        /// <summary>
        ///解析K206题型 
        /// </summary>
        /// <param name="id">paperDetailId</param>
        /// <param name="element">节点元素</param>
        /// <returns>每题的答案</returns>
        private static TestDetail SetAnswerByK206(string id,string testlibraryId,HtmlElement element)
        {
            //int tempId;
            //string temp="";

            //HtmlElementCollection collection = element.GetElementsByTagName("div");
            //foreach (HtmlElement item in collection)
            //{
            //    if (int.TryParse(item.GetAttribute("id").ToString(), out tempId))
            //    {
            //        HtmlElementCollection inputcollection = item.GetElementsByTagName("input");
            //        foreach (HtmlElement input in inputcollection)
            //        {
            //            if (Convert.ToBoolean(input.GetAttribute("checked")) == true)
            //            {
            //                temp += input.GetAttribute("value").Substring(0, 1)+'}';                          
            //            }
            //        }
            //    }
                
            //}
            HtmlElement element2 = element.GetElementsByTagName("input")[0];
            string answer = element2.GetAttribute("value");
            TestDetail detail = new TestDetail {Answer = answer, PapersDetailId = id,TestLibraryId = testlibraryId};
            return detail;
        }

        /// <summary>
        ///解析K401题型 ,K103 ,K201
        /// </summary>
        /// <param name="id">paperDetailId</param>
        /// <param name="element">节点元素</param>
        /// <returns>每题的答案</returns>
        private static TestDetail SetAnswerK401(string id,string testlibraryId,HtmlElement element)
        {
            //string temp = "";
            //HtmlElementCollection inputcollection = element.GetElementsByTagName("input");
            //foreach (HtmlElement input in inputcollection)
            //{
            //    if (Convert.ToBoolean(input.GetAttribute("checked")) == true)
            //    {
            //       // MessageBox.Show(input.GetAttribute("value"));
            //        temp+= input.GetAttribute("value").Substring(1, 1)+"}";
                    
            //    }
            //}
            HtmlElement element2 = element.GetElementsByTagName("input")[0];
            string answer = element2.GetAttribute("value");
            TestDetail detail = new TestDetail { Answer = answer, PapersDetailId = id, TestLibraryId = testlibraryId };
            return detail;
        }

        /// <summary>
        /// 解析K202题型
        /// </summary>
        /// <param name="id"></param>
        /// <param name="testlibraryId"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        private static TestDetail SetAnswerK202(string id, string testlibraryId, HtmlElement element)
        {
            //string temp = "";
            //HtmlElementCollection inputcollection = element.GetElementsByTagName("input");
            //foreach (HtmlElement input in inputcollection)
            //{
            //    if (Convert.ToBoolean(input.GetAttribute("checked")) == true)
            //    {             
            //        temp += input.GetAttribute("value").Substring(0, 1) + "}";

            //    }
            //}
            HtmlElement element2 = element.GetElementsByTagName("input")[0];
            string answer = element2.GetAttribute("value");
            TestDetail detail = new TestDetail { Answer = answer, PapersDetailId = id, TestLibraryId = testlibraryId };
            return detail;
        }

        /// <summary>
        /// 解析201题型
        /// </summary>
        /// <param name="id"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        private static TestDetail SetAnswerK102(string id,string testlibraryId, HtmlElement element)
        {
            //string temp = "";
            //HtmlElementCollection inputcollection = element.GetElementsByTagName("input");
            //foreach (HtmlElement input in inputcollection)
            //{
            //    if (Convert.ToBoolean(input.GetAttribute("checked")) == true)
            //    {
            //        temp += input.GetAttribute("value").Substring(1, 1);

            //    }
            //}
            HtmlElement element2 = element.GetElementsByTagName("input")[0];
            string answer = element2.GetAttribute("value");
            TestDetail detail = new TestDetail { Answer = answer, PapersDetailId = id, TestLibraryId = testlibraryId };
            return detail;
        }

        /// <summary>
        /// 解析K503题型 ，K701,K702
        /// </summary>
        /// <param name="id"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        private static TestDetail SetAnswerK503(string id,string testlibraryId,HtmlElement element)
        {
            //HtmlElementCollection inputcollection = element.GetElementsByTagName("input");
            //string temp=inputcollection[0].GetAttribute("value");
            HtmlElement element2 = element.GetElementsByTagName("input")[0];
            string answer = element2.GetAttribute("value");
            TestDetail detail = new TestDetail { Answer = answer, PapersDetailId = id, TestLibraryId = testlibraryId };
            return detail;
        }

        /// <summary>
        /// 解析K403题型，K204,K409
        /// </summary>
        /// <param name="id"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        private static TestDetail SetAnswerK403(string id,string testlibraryId, HtmlElement element)
        {
            //string temp = "";
            //HtmlElementCollection inputcollection = element.GetElementsByTagName("input");
            //foreach (HtmlElement htmlElement in inputcollection)
            //{
            //     temp+= inputcollection[0].GetAttribute("value")+"}";
            //}
            HtmlElement element2 = element.GetElementsByTagName("input")[0];
            string answer = element2.GetAttribute("value");

            TestDetail detail = new TestDetail { Answer = answer, PapersDetailId = id, TestLibraryId = testlibraryId };
            return detail;
        }
        /// <summary>
        /// 解析K802题型,K801
        /// </summary>
        /// <param name="id"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        private static TestDetail SetAnswerK802(string id,string testlibraryId,HtmlElement element)
        {
            //HtmlElementCollection textarea = element.GetElementsByTagName("textarea");
            //string temp = textarea[0].GetAttribute("value");
            HtmlElement element2 = element.GetElementsByTagName("input")[0];
            string answer = element2.GetAttribute("value");
            TestDetail detail = new TestDetail { Answer = answer, PapersDetailId = id, TestLibraryId = testlibraryId };
            return detail;
        }  
    }
}